package com.harlan.viewbindingstudy;

import android.content.Context;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class HeadAndFootRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<View> headList = new ArrayList<>();
    List<View> footList = new ArrayList<>();
    List<String> list = new ArrayList<>();
    private Context context;

    public HeadAndFootRecyclerViewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = View.inflate(context, R.layout.head_main, null);
            HeadViewHolder myHeadViewHolder = new HeadViewHolder(view);
            return myHeadViewHolder;
        } else if (viewType == 1) {
            View view = View.inflate(context, R.layout.viewhold_item, null);
            ItemViewHolder myViewHolder = new ItemViewHolder(view);
            return myViewHolder;
        } else {
            View view = View.inflate(context, R.layout.foot_main, null);
            FootViewHolder myFootViewHolder = new FootViewHolder(view);
            return myFootViewHolder;
        }

    }

    public void setData(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {
            if (((HeadViewHolder) holder).head_layout.getChildCount() == 0) {
                ((HeadViewHolder) holder).head_layout.addView(headList.get(position));
            }
        } else if (holder instanceof ItemViewHolder) {
            ((ItemViewHolder) holder).tv.setText(list.get(position - headList.size()));
        } else {
            if (((FootViewHolder) holder).foot_layout.getChildCount() == 0) {
                ((FootViewHolder) holder).foot_layout.addView(footList.get(position - headList.size() - list.size()));
            }
        }

    }

    @Override
    public int getItemCount() {
        return list.size() + headList.size() + footList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position < headList.size()) {
            return 0;
        } else if (position < headList.size() + list.size()) {
            return 1;
        } else {
            return 2;
        }
    }

    public void addHeadView(View view) {
        if (headList != null) {
            headList.add(view);
        }
    }

    public void addFootView(View view) {
        if (footList != null) {
            footList.add(view);
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.itemname);
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        LinearLayout head_layout;

        public HeadViewHolder(View itemView) {
            super(itemView);
            head_layout = (LinearLayout) itemView.findViewById(R.id.head_layout);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        LinearLayout foot_layout;

        public FootViewHolder(View itemView) {
            super(itemView);
            foot_layout = (LinearLayout) itemView.findViewById(R.id.foot_layout);
        }
    }


}
